<?php

namespace App\Models\DyModels;

use App\Models\ParentModel;
use DB;

/**
 * 抖音企业号粉丝表
 * @author puppet
 */
class DyuserQyhFans extends ParentModel
{
    /**
     * 与模型关联的数据表。
     *
     * @var string
     */
    protected $table = 'pt_fans';

    /**
     * DB::直接操作的数据库表名
     * @var string
     */
    protected $db_table = 'pt_fans';

    /**
     * 主键 默认id。
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 指定是否模型应该被戳记时间。
     *
     * @var bool
     */
    public $timestamps = false;

    //查询视频是新数据还是已有需要更新的数据$open_id 和视频id都一样就
    function getFansId($open_id, $popen_id)
    {
        $sql = " select id from $this->table where p_open_id='$popen_id' and open_id='$open_id'";
        $res = DB::select($sql);
        return $res;
    }
    //存入用户 open_id 相关联的关注数据
    function upDyUserFollowing($arr,$p_open_id){
        foreach($arr as $k=>$v){
            $open_id=isset($v['open_id']) ? $v['open_id'] : '';
            $re=DB::select("select id from pt_following where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);
            $re=isset($re[0]['id']) ? $re[0]['id'] : false;
            $data=[
                'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
                'city'=>isset($v['city']) ? $v['city'] : '',
                'country'=>isset($v['country']) ? $v['country'] : '',
                'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
                'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
                'province'=>isset($v['province']) ? $v['province'] : '',
                'union_id'=>isset($v['union_id']) ? $v['union_id'] : '',
                'update_time'=>date('Y-m-d H:i:s',time()),
                'is_follower'=>1,
            ];
            if($re){
                DB::table('pt_following')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
            }else{
                $data['open_id']=$open_id;
                $data['p_open_id']=$p_open_id;
                $data['create_time']=date('Y-m-d H:i:s',time());
                DB::table('pt_following')->insert($data);
            }
        }
        DB::update("update pt_following set `is_follower`=0,`update_time`='".date('Y-m-d')."' where p_open_id='{$p_open_id}' and update_time<'".date('Y-m-d')."'");
    }

    //存入用户 open_id 相关的意向客户
    function upDyUserEnterprise($arr,$p_open_id){
        foreach($arr as $k=>$v){
            $leads_level=isset($v['leads_level']) ? $v['leads_level'] : -1;
            $open_id=isset($v['open_id']) ? $v['open_id'] : '';
            $re=DB::select("select id from pt_enterprise where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);//查询是否已经有数据了
            $re=isset($re[0]['id']) ? $re[0]['id'] : false;
            $data=[
                'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
                'leads_level'=>$leads_level,
                'telephone'=>isset($v['telephone']) ? $v['telephone'] : '',
                'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
                'city'=>isset($v['city']) ? $v['city'] : '',
                'is_follow'=>isset($v['is_follow'])  ? 1 : 0,
                'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
//				'wechat'=>isset($v['wechat']) ? $v['wechat'] : '',
//				'is_follow'=>isset($v['is_follow']) ? $v['age'] : 0,
                'update_time'=>date('Y-m-d H:i:s',time()),
            ];
            if($re){
                DB::table('pt_enterprise')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
            }else{
                $data['open_id']=$open_id;
                $data['p_open_id']=$p_open_id;
                $data['create_time']=date('Y-m-d H:i:s',time());
                if($leads_level==1 || $leads_level==2){
                    DB::table('pt_enterprise')->insert($data);
                }
            }
        }
    }
    //存入用户 open_id 相关联的粉丝数据
    function upDyUserFans($arr,$p_open_id){
        foreach($arr as $k=>$v){
            $open_id=isset($v['open_id']) ? $v['open_id'] : '';
            $re=DB::select("select id from pt_fans where p_open_id='{$p_open_id}' and open_id=?",[$open_id]);
            $re=isset($re[0]['id']) ? $re[0]['id'] : false;
            $data=[
                'avatar'=>isset($v['avatar']) ? $v['avatar'] : '',
                'city'=>isset($v['city']) ? $v['city'] : '',
                'country'=>isset($v['country']) ? $v['country'] : '',
                'gender'=>isset($v['gender']) ? ($v['gender']==0 ? '未知' : ($v['gender']==1 ? '男' : '女')) : '',
                'nickname'=>isset($v['nickname']) ? htmlspecialchars($v['nickname'],ENT_QUOTES) : '',
                'province'=>isset($v['province']) ? $v['province'] : '',
                'union_id'=>isset($v['union_id']) ? $v['union_id'] : '',
                'update_time'=>date('Y-m-d H:i:s',time()),
                'is_follower'=>1,
            ];
            if($re){
                DB::table('pt_fans')->where('open_id',$open_id)->where('p_open_id',$p_open_id)->update($data);
            }else{
                $data['open_id']=$open_id;
                $data['p_open_id']=$p_open_id;
                $data['create_time']=date('Y-m-d H:i:s',time());
                DB::table('pt_fans')->insert($data);
            }
        }
        DB::update("update pt_fans set `is_follower`=0,`update_time`='".date('Y-m-d')."' where p_open_id='{$p_open_id}' and update_time<'".date('Y-m-d')."'");
    }
    //存入抖音企业号粉丝数据
    function saveFansInfo($date, $open_id)
    {
        foreach ($date as $k => $v) {
            $id = $this->getFansId($v['open_id'], $open_id);
            if (empty($id)) {//查询不到表示没有重复的
                $data = array(
                    'is_follower' => $v['is_follower'],
                    'open_id' => $v['open_id'],
                    'union_id' => $v['union_id'],
                    'nickname' => isset($v['nickname']) ? htmlspecialchars($v['nickname'], ENT_QUOTES) : '',
                    'avatar' => $v['avatar'],
                    'city' => $v['city'],
                    'province' => $v['province'],
                    'country' => $v['country'],
                    'gender' => isset($v['gender']) ? ($v['gender'] == 0 ? '未知' : ($v['gender'] == 1 ? '男' : '女')) : '',
                    'create_time' => date('Y-m-d H:i:s', $v['create_time']),
                    'update_time' => date('Y-m-d H:i:s', time()),
                    'p_open_id' => $open_id,
                );
                DB::table("pt_fans")->insertGetId($data);
            }else{//更新粉丝数据
                $data=array(
                    'is_follower' => $v['is_follower'],
                    'union_id' => $v['union_id'],
                    'nickname' => isset($v['nickname']) ? htmlspecialchars($v['nickname'], ENT_QUOTES) : '',
                    'avatar' => $v['avatar'],
                    'city' => $v['city'],
                    'province' => $v['province'],
                    'country' => $v['country'],
                    'gender' => isset($v['gender']) ? ($v['gender'] == 0 ? '未知' : ($v['gender'] == 1 ? '男' : '女')) : '',
                    'update_time' => date('Y-m-d H:i:s', time()),
                );
                $sql=" update {$this->table} set ";
                foreach($data as $k=>$v){
                    $sql.="`{$k}`='{$v}',";
                }
                $sql=substr($sql,0,-1)." where id={$id[0]['id']}";
                DB::update($sql);

            }
        }
    }
}